package com.itheima.service.mongo.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import com.itheima.domain.mongo.UserLocation;
import com.itheima.service.mongo.UserLocationService;
import org.apache.dubbo.config.annotation.DubboService;
import org.bson.types.ObjectId;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.geo.Circle;
import org.springframework.data.geo.Distance;
import org.springframework.data.geo.Metrics;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.geo.GeoJsonPoint;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;

import java.util.ArrayList;
import java.util.List;

/**
 * @Author 请不要叫我高司令
 * @Date 2022/3/20 23:52
 * @Version 1.0
 */

@DubboService
public class UserLocationServiceImpl implements UserLocationService {

    @Autowired
    private MongoTemplate mongoTemplate;

    //上传地理位置
    @Override
    public void saveLocation(Double longitude, Double latitude, String addrStr, Long userId) {

        //先查
        UserLocation userLocation = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class);

        //判断有没有，有更新，没有新增
        if (ObjectUtil.isEmpty(userLocation)) {

            userLocation = new UserLocation();
            userLocation.setUserId(userId);
            userLocation.setLocation(new GeoJsonPoint(longitude, latitude));
            userLocation.setAddress(addrStr);
            userLocation.setCreated(System.currentTimeMillis());
            userLocation.setUpdated(System.currentTimeMillis());


        } else {

            userLocation.setLocation(new GeoJsonPoint(longitude, latitude));
            userLocation.setAddress(addrStr);
            userLocation.setUpdated(System.currentTimeMillis());

        }

        mongoTemplate.save(userLocation);




    }

    //搜附近
    @Override
    public List<Long> searchNearUser(Long userId, Long distance) {

        //找圆心（自己的坐标）
        GeoJsonPoint location = mongoTemplate.findOne(new Query(Criteria.where("userId").is(userId)), UserLocation.class).getLocation();


        //指定半径
        Distance dis = new Distance(distance / 1000, Metrics.KILOMETERS);


        //画圆
        Circle circle = new Circle(location, dis);

        //条件搜索
        Query query = new Query(Criteria.where("location").withinSphere(circle));

        List<UserLocation> userLocationList = mongoTemplate.find(query, UserLocation.class);

        //遍历返回id列表
        ArrayList<Long> nearUserIdList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(userLocationList)) {

            for (UserLocation userLocation : userLocationList) {
                nearUserIdList.add(userLocation.getUserId());
            }

        }

        return nearUserIdList;

    }

    //查找用户的地理位置名称
    public String findLocation(Long userId) {
        Query query = new Query(
                Criteria.where("userId").is(userId)
        );
        UserLocation userLocation = mongoTemplate.findOne(query, UserLocation.class);

        if (userLocation == null) {

            return null;            //TODO我改了一下

        }
        return userLocation.getAddress();
    }

}
